package centaline.api.utils;


import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import java.net.URLDecoder;


public class HttpUtil {

	private static final String APPLICATION_JSON = "application/json";
	private static final String CONTENT_TYPE_TEXT_JSON = "text/json";


	//HttpPost请求
	public static void doPost(String url, String json) throws Exception {
		// 将JSON进行UTF-8编码,以便传输中文
		//String encoderJson = URLEncoder.encode(json, "utf-8");

		CloseableHttpClient httpclient = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost(url);
		httpPost.addHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON);
		//post设置json参数
		StringEntity se = new StringEntity(json);
		se.setContentType(CONTENT_TYPE_TEXT_JSON);
		se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON));
		httpPost.setEntity(se);

		HttpResponse res = httpclient.execute(httpPost);
		String jsonStr = EntityUtils.toString(res.getEntity(), "utf-8");
		System.out.println(JSONObject.parseObject(jsonStr));
	}

	//HttpGet请求
	public static HttpResponse doGet(String url, String json) throws Exception {
		System.out.println("json=="+json);
		CloseableHttpClient httpclient = HttpClients.createDefault();

		HttpGet  httpGet = new HttpGet(url+"?"+ URLDecoder.decode(json, "utf-8"));
		httpGet.addHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON);

		HttpResponse httpResponse = httpclient.execute(httpGet);

		String conResult = EntityUtils.toString(httpResponse.getEntity());
		System.out.println(JSONObject.parseObject(conResult));
		return httpResponse;
	}



	public static void main(String arg[]) throws Exception {
		//String url = "http://10.28.11.95:8083/trade-web/rest/getAccessToken";
		//String url = "https://a.sh.centanet.com/trade-web/rest/getAccessToken";
		String url = "http://gfbapp.vcash.cn/appweb/data/ws/rest/orderstatus/query";
		JSONObject params1 = new JSONObject();
		JSONObject params2 = new JSONObject();
		//params1.put("clientId", "Apass");
		//params1.put("securityCode", "cooperCompany20170801");
		//doPost(url, params1.toString());

		params2.put("jujian_code","123456");
		String encryptKey = "testKEY@20170815";
		String jujian_code = "";//AESUtils.aesEncrypt(params2.toString(),encryptKey);
		params1.put("requestData", jujian_code);
		doPost(url, params1.toString());
		//HttpGet请求 时传递的参数是通过 EntityUtils.toString(new UrlEncodedFormEntity(params, Consts.UTF_8)) 这种方式变更为？A=123&B=456
		//String url2 = "http://10.28.11.95:8083/trade-web/rest/cases/CSZ-20129760";
		//List<NameValuePair> params = new ArrayList<NameValuePair>();
		//params.add(new BasicNameValuePair("token", "bqOBb/6uijSTCaliedd5I2kWUO2LUBluJ8eazrX7IsgsAc75mvJwuptslJNcxrWY"));

		//System.out.println("params="+EntityUtils.toString(new UrlEncodedFormEntity(params, Consts.UTF_8)));
		//doGet(url2, EntityUtils.toString(new UrlEncodedFormEntity(params, Consts.UTF_8)));


	}
}
